﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MM.Common.DBBase;
using System.Data;
using System.Windows.Forms;

namespace MM.Common.Base
{
    public static class ConfigFunction
    {
        // Methods
        public static bool CreateComputerConfig(string paraname, object paravalue, string paratype, string title, string describe)
        {
            string commandtext = "INSERT INTO MXHLY_SYS_PARAMETER_COMPUTER (COMPUTER_CODE, PARAMETER_NAME, PARAMETER_VALUE, PARAMETER_TYPE, PARAMETER_NAME_CHN, DESCRIBE)     VALUES ( :node, :paraname, :paravalue, :paratype, :title, :describe)";
            DataParamCol parms = new DataParamCol();
            parms.AddBatch(new object[] { "node", SystemInfo.ComputerID, "paraname", paraname, "paravalue", paravalue.ToString(), "paratype", paratype, "title", title, "describe", describe });
            return (SystemInfo.DbData.ExecuteSql(commandtext, parms) == 1);
        }

        public static bool CreateEmpConfig(string paraname, object paravalue, string paratype, string title, string describe)
        {
            string commandtext = "INSERT INTO MXHLY_SYS_PARAMETER_EMP (EMP_ID, PARAMETER_NAME, PARAMETER_VALUE, PARAMETER_TYPE, PARAMETER_NAME_CHN, DESCRIBE)     VALUES ( :emp_id, :paraname, :paravalue, :paratype, :title, :describe) ";
            DataParamCol parms = new DataParamCol();
            parms.AddBatch(new object[] { "emp_id", SystemInfo.OperId, "paraname", paraname, "paravalue", paravalue.ToString(), "paratype", paratype, "title", title, "describe", describe });
            return (SystemInfo.DbData.ExecuteSql(commandtext, parms) == 1);
        }

        public static bool CreateSystemConfig(string paraname, object paravalue, string paratype, string title, string modify_type, string restrict_type, string describe)
        {
            string commandtext = "INSERT INTO MXHLY_SYS_PARAMETER (PARAMETER_NAME, PARAMETER_VALUE, PARAMETER_TYPE, PARAMETER_NAME_CHN, MODIFY_TYPE, RESTRICT_TYPE ,DESCRIBE)     VALUES (:parameter_name , :parameter_value , :parameter_type , :parameter_name_chn , :modify_type , :restrict_type , :describe) ";
            DataParamCol parms = new DataParamCol();
            parms.Add("parameter_name", paraname);
            parms.Add("parameter_value", paravalue.ToString());
            parms.Add("parameter_type", paratype);
            parms.Add("parameter_name_chn", title);
            parms.Add("modify_type", modify_type);
            parms.Add("restrict_type", restrict_type);
            parms.Add("describe", describe);
            int num = SystemInfo.DbData.ExecuteSql(commandtext, parms);
            if (num == -1)
            {
                MessageBox.Show("新增参数失败:" + SystemInfo.DbData.ErrorMessage, "错误");
            }
            return (num == 1);
        }

        public static string GetComputerConfig(string varname)
        {
            string str = "";
            string commandtext = "SELECT PARAMETER_VALUE FROM MXHLY_SYS_PARAMETER_COMPUTER WHERE COMPUTER_CODE = :node AND PARAMETER_NAME = :paraname ";
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("node", SystemInfo.ComputerID), new InputDataParam("paraname", varname) });
            object obj2 = null;
            SystemInfo.DbData.ExecuteScalar(commandtext, parms, ref obj2);
            if ((obj2 != null) && !Convert.IsDBNull(obj2))
            {
                str = obj2.ToString();
            }
            return str;
        }

        public static string GetComputerConfig(string varname, object defaultValue, string paramTitle)
        {
            string computerConfig = GetComputerConfig(varname);
            switch (computerConfig)
            {
                case null:
                case "":
                    computerConfig = "";
                    CreateComputerConfig(varname, defaultValue, "C", paramTitle, paramTitle);
                    break;
            }
            return computerConfig;
        }

        public static string GetEmpConfig(string varname)
        {
            string str = "";
            string commandtext = "SELECT PARAMETER_VALUE FROM MXHLY_SYS_PARAMETER_EMP WHERE EMP_ID = :operid AND PARAMETER_NAME = :paraname";
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("operid", SystemInfo.OperId), new InputDataParam("paraname", varname) });
            object obj2 = null;
            SystemInfo.DbData.ExecuteScalar(commandtext, parms, ref obj2);
            if ((obj2 != null) && !Convert.IsDBNull(obj2))
            {
                str = obj2.ToString();
            }
            return str;
        }

        public static string GetEmpConfig(string varname, object defaultValue, string paramTitle, string para_type)
        {
            string empConfig = GetEmpConfig(varname);
            switch (empConfig)
            {
                case null:
                case "":
                    empConfig = defaultValue.ToString();
                    CreateEmpConfig(varname, defaultValue, para_type, paramTitle, "");
                    break;
            }
            return empConfig;
        }

        public static string GetOldSystemConfig(string prog, string paraname)
        {
            string oracleUser = GetOracleUser(SystemInfo.OrgSubShortCode);
            string str2 = "";
            if (!string.IsNullOrEmpty(oracleUser))
            {
                string commandtext = "SELECT VVALUE FROM " + oracleUser + ".SYSVAR WHERE PROG =:prog and VNAME = :paraname";
                DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("prog", prog), new InputDataParam("paraname", paraname) });
                DataTable dt = new DataTable();
                if (SystemInfo.DbData.Retrieve(commandtext, parm, ref dt) && (dt.Rows.Count == 1))
                {
                    str2 = dt.Rows[0]["VVALUE"].ToString();
                }
            }
            return str2;
        }

        public static string GetOracleUser(string code)
        {
            string str = "";
            string commandtext = "SELECT USERBZ FROM FDMCB WHERE DM = :dm";
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("dm", code) });
            object obj2 = null;
            SystemInfo.DbData.ExecuteScalar(commandtext, parms, ref obj2);
            if ((obj2 != null) && !Convert.IsDBNull(obj2))
            {
                str = obj2.ToString();
            }
            return str;
        }

        public static string GetSystemConfig(string varname)
        {
            string systemConfigBase = GetSystemConfigBase(varname + "$" + SystemInfo.OrgSubShortCode);
            if (systemConfigBase == string.Empty)
            {
                systemConfigBase = GetSystemConfigBase(varname);
            }
            return systemConfigBase;
        }

        public static string GetSystemConfig(string varname, object defaultValue, string paramTitle, string para_type)
        {
            string systemConfig = GetSystemConfig(varname);
            switch (systemConfig)
            {
                case null:
                case "":
                    systemConfig = defaultValue.ToString();
                    CreateSystemConfig(varname, defaultValue, para_type, paramTitle, "Y", "SYSTEM", "");
                    break;
            }
            return systemConfig;
        }

        public static string GetSystemConfigBase(string varname)
        {
            string str = "";
            string commandtext = "SELECT PARAMETER_VALUE ,RESTRICT_TYPE FROM MXHLY_SYS_PARAMETER WHERE PARAMETER_NAME = :paraname";
            DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("paraname", varname) });
            DataTable dt = new DataTable();
            if (SystemInfo.DbData.Retrieve(commandtext, parm, ref dt) && (dt.Rows.Count == 1))
            {
                str = dt.Rows[0]["PARAMETER_VALUE"].ToString();
                string str5 = dt.Rows[0]["RESTRICT_TYPE"].ToString();
                if (str5 == null)
                {
                    return str;
                }
                if (!(str5 == "PERSONAL"))
                {
                    if (str5 == "COMPUTER")
                    {
                        string computerConfig = GetComputerConfig(varname);
                        if (computerConfig != "")
                        {
                            str = computerConfig;
                        }
                    }
                    return str;
                }
                string empConfig = GetEmpConfig(varname);
                if (empConfig != "")
                {
                    str = empConfig;
                }
            }
            return str;
        }

        public static bool SetComputerConfig(string varname, object varvalue)
        {
            string commandtext = "UPDATE MXHLY_SYS_PARAMETER_COMPUTER SET PARAMETER_VALUE = :varvalue WHERE COMPUTER_CODE = :node AND PARAMETER_NAME = :paraname";
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("varvalue", varvalue.ToString()), new InputDataParam("node", SystemInfo.ComputerID), new InputDataParam("paraname", varname) });
            return (SystemInfo.DbData.ExecuteSql(commandtext, parms) == 1);
        }

        public static bool SetComputerConfig(string varname, object varvalue, string paramTitle)
        {
            string computerConfig = GetComputerConfig(varname);
            switch (computerConfig)
            {
                case null:
                case "":
                    computerConfig = "";
                    CreateComputerConfig(varname, varvalue, "C", paramTitle, paramTitle);
                    return false;
            }
            string commandtext = "UPDATE MXHLY_SYS_PARAMETER_COMPUTER SET PARAMETER_VALUE = :varvalue WHERE COMPUTER_CODE = :node AND PARAMETER_NAME = :paraname";
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("varvalue", varvalue.ToString()), new InputDataParam("node", SystemInfo.ComputerID), new InputDataParam("paraname", varname) });
            return (SystemInfo.DbData.ExecuteSql(commandtext, parms) == 1);
        }

        public static bool SetEmpConfig(string varname, object varvalue)
        {
            string commandtext = "UPDATE MXHLY_SYS_PARAMETER_EMP SET PARAMETER_VALUE = :varvalue WHERE EMP_ID = :operid AND PARAMETER_NAME = :paraname";
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("varvalue", varvalue.ToString()), new InputDataParam("operid", SystemInfo.OperId), new InputDataParam("paraname", varname) });
            return (SystemInfo.DbData.ExecuteSql(commandtext, parms) == 1);
        }

        public static bool SetSystemConfig(string varname, object varvalue)
        {
            string commandtext = "UPDATE MXHLY_SYS_PARAMETER SET PARAMETER_VALUE = :paravalue WHERE PARAMETER_NAME = :paraname";
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("paravalue", varvalue), new InputDataParam("paraname", varname) });
            return (SystemInfo.DbData.ExecuteSql(commandtext, parms) == 1);
        }
    }
}
